package net.dodoman.regedit;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MACIPTest {

	public static void main(String[] args) {
		String osName = System.getProperty("os.name");
		StringBuffer systemPathBuff = new StringBuffer("");
		if (osName.indexOf("Windows") > -1) {
			// Windows操作系统的cmd.exe的绝对路径
			// systemPathBuff.append();
			systemPathBuff.append("c:/WINDOWS/system32/cmd.exe");
		}
		if (osName.indexOf("NT") > -1) {
			// NT操作系统的cmd.exe的绝对路径
			systemPathBuff.append("c:/WINDOWS/command.com");
		}
		// 
		Process pro = null;
		try {
			// Serial Number 的得到
			// 此语句相当于在cmd下面执行 dir的命令.并得到命令执行完毕后的输出流
			pro = Runtime.getRuntime().exec(
					systemPathBuff.toString() + " /c dir");
			InputStream is = pro.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			// 读取第一行
			String message = br.readLine();
			// Serial Number 的零时变量
			int index = -1;
			pro = Runtime.getRuntime().exec(
					systemPathBuff.toString() + " /c ipconfig/all");
			is = pro.getInputStream();
			br = new BufferedReader(new InputStreamReader(is));
			// 读取第一行
			message = br.readLine();
			// Mac地址 的零时变量
			String macResult = null;
			while (message != null) {
				if ((index = message.indexOf("Physical Address")) > 0) {
					macResult = message.substring(index + 36).trim();
					break;
				}
				// 读取下一行
				message = br.readLine();
			}
			System.out.println(macResult);
			// 读取下一行
			message = br.readLine();
			// Mac地址和IP地址 的得到
			// IP地址 的零时变量
			String ipResult = null;
			while (message != null) {
				if ((index = message.indexOf("IP Address")) > 0) {
					ipResult = message.substring(index + 36).trim();
					break;
				}
				// 读取下一行
				message = br.readLine();
			}
			System.out.println(ipResult);
			// 读入流的关闭
			br.close();
			pro.destroy();
		} catch (IOException e) {
			System.out.println("Can''t get mac address!");
		}
	}
}
